<!--
 * @Author: LiYuan
 * @Date: 2021-03-31 16:10:21
 * @LastEditTime: 2022-05-13 15:42:19
 * @LastEditors: lily
 * @Description: 取消说明
 * You build it, You run it.
-->
<template>
  <base-modal :title="$t('event.detail.components.dialog.text39')" :show.sync="visible" width="600px" class="event-off-dialog">
    <div class="base-modal-content">
      <!-- <el-input v-model="reason" type="textarea" placeholder="请输入取消说明[最多500字][必填]" maxlength="500"></el-input> -->
      <el-form label-position="top" :model="form" :rules="rules" ref="dialogForm" class="dialog-form">
        <el-form-item :label="$t('task.detail.components.cancelDescription')" prop="reason">
          <el-input v-model="form.reason" type="textarea" maxlength="500" :placeholder="$t('event.detail.components.dialog.text40')"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button @click="visible = false">{{$t('common.base.cancel')}}</el-button>
      <el-button type="primary" @click="handleConfirm" :loading="loading">{{$t('common.base.makeSure')}}</el-button>
    </div>
  </base-modal>
</template>

<script>
import * as EventApi from '@src/api/EventApi.js';
export default {
  name: 'event-off-dialog',
  //   inject: ['initData'],
  props:{
    workEvent: {
      type: Object,
      default: () => {}
    },
    getVipApproveFunction: {
      type: Function,
      default: (() => {})
    }
  },
  data() {
    return {
      rules: {
        reason: [
          { required: true, message: this.$t('event.detail.components.dialog.text41'), trigger: 'blur' },
        ]
      },
      form: {
        reason: ''
      },
      reason: '',
      visible: false,
      loading: false
    }
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    handleConfirm() {
      this.$refs['dialogForm'].validate(async (vali) => {
        if (vali) {
          const params = {
            content: {
              updateType: 'eRecord',
              updateContent: this.form.reason,
            },
            event: {
              id: this.workEvent.id
            }
          }
          this.loading = true

          // 判断是否有高级审批(有审批或者接口报错，return)
          try {
            const result = await this.getVipApproveFunction('completeCancel', params)
            if(result?.needVipApprove) {
              this.visible = false
              this.$emit('needVipApprove', result);
              return;
            }
          } catch (e) {
            console.error(e)
            return;
          } finally {
            this.loading = false;
          }

          EventApi.eventOff(params)
              .then(res => {
                // 取消操作需要审批操作
                // TODO 需要审批判断改成errorCode
                if (!res.succ && Number(res.errorCode) == 10003) {
                  this.$emit('needApprove', res.data);
                  this.visible = false
                }
                if (res.status == 0) {
                  this.$emit('success')
                  this.visible = false
                  window.location.href = `${this.$resourcePrefix}/event/view/${this.workEvent.id}`;
                }
              })
              .finally(() => {
                this.loading = false
              })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.base-modal-content {
  padding: 20px;
}
</style>